﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using SQLExecute;


public partial class reports_print_profile : System.Web.UI.Page
{
    ReportDocument rptDoc = new ReportDocument();
    ParameterField paramField = new ParameterField();
    ParameterFields paramFields = new ParameterFields();
    ParameterDiscreteValue paramDiscretevalues = new ParameterDiscreteValue();
    ParameterValues paramValues = new ParameterValues();
    private const string PARAMETER_FIELD_NAME = "profile_id";
    protected void Page_Load(object sender, EventArgs e)
    {
       // LoadReport();
        Load_DataSet2Report();
    }

    private void LoadReport()
    {

        if (Request.Params["id"] != null)
        {
            paramField.Name = "@profile_id";
            //paramDiscretevalues.Value = p.ID.ToString();
            paramDiscretevalues.Value = Request.QueryString["id"].ToString();
            paramField.CurrentValues.Add(paramDiscretevalues);
            paramFields.Add(paramField);
            rptViewProfile.ParameterFieldInfo = paramFields;

            rptDoc.Load(Server.MapPath("~/reports/rpt_profile.rpt"));


            Common.SetConnectionInfo(rptDoc.Database.Tables);
            rptViewProfile.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
            rptViewProfile.ReportSource = rptDoc;
            Session["report"] = rptDoc;
            //Session.Add(
        }
        else
        {
            Response.Redirect(Server.MapPath("~/list_profiles.aspx"));
        }

        //return rptDoc;
    }

    protected override void OnUnload(EventArgs e)
    {

        base.OnUnload(e);
        this.Unload += new EventHandler(Page_Unload);

    }
    public void Page_Unload(object sender, EventArgs e)
    {

        // clean up the resources

        rptDoc.Close();
        rptDoc.Dispose();
        GC.Collect();


    }

    protected void btnPrint_Click(object sender, EventArgs e)
    {
        if (Request.Params["id"] != null)
        {
            Load_DataSet2Report();
        }
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string strImageProfile;
        Profiles p=new Profiles();
        //DataSet ds = new DataSet();
        if (Request.Params["id"] != null)
        {
            
            profile ds = new profile();
            
            string strID = Request.QueryString["id"].ToString();
            Guid id = new Guid(strID);
            p.ID = id;
            //ds = p.Get_Profile();
            //ds.Tables[0].Columns.Add("image_profile", Type.GetType("System.Byte[]"));
            /*
            if (ds.Tables[0].Rows[0]["image_URL"].ToString() != "")
            {
                strImageProfile = HttpContext.Current.Server.MapPath(ds.Tables[0].Rows[0]["image_URL"].ToString());
                if (File.Exists(strImageProfile))
                    LoadImage(ds.Tables[0].Rows[0], "image_profile", strImageProfile);
            }*/
            //ds.WriteXmlSchema(HttpContext.Current.Server.MapPath("~/getProfile.xsd"));
            //rptDoc = new ReportDocument();
            //rptDoc.Load(HttpContext.Current.Server.MapPath("~/reports/rpt_profile8.rpt"));
            //rptDoc.SetDataSource(ds.Tables[0]);
            //ds = SqlHelper.ExecuteDataset(Common.strConnect, "Get_Profile", new object[] {id});

            
            SqlConnection cn = new SqlConnection(Common.strConnect);
            SqlCommand sqlCommand = new SqlCommand("Get_Profile", cn);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter param = sqlCommand.Parameters.Add("profile_id", SqlDbType.UniqueIdentifier);
            param.Value = id;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            sqlDataAdapter.Fill(ds.Tables[0]);

            if (ds.Tables[0].Rows[0]["image_URL"].ToString() != "")
            {
                string s = Server.MapPath(ds.Tables[0].Rows[0]["image_URL"].ToString());

                if (File.Exists(s))
                    LoadImage(ds.Tables[0].Rows[0], "image_stream", s);
            }


           // rptDoc = new ReportDocument();
           // rptDoc.Load(Server.MapPath("~/reports/rpt_profile8.rpt"));
           // rptDoc.SetDataSource(ds.Tables[0]);
           // rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("~/export/profile.pdf"));
            //ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/profile.pdf');popup.focus();", true);
        }

    }
    protected void btnExport_Click2(object sender, EventArgs e)
    {
        if (Request.Params["id"] != null)
        {

            paramField.Name = "@profile_id";
            paramDiscretevalues.Value = Request.QueryString["id"].ToString();
            paramField.CurrentValues.Add(paramDiscretevalues);
            paramFields.Add(paramField);
            rptViewProfile.ParameterFieldInfo = paramFields;
            //rptViewProfile.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
            rptDoc.Load(Server.MapPath("~/reports/rpt_profile5.rpt"));
            Common.SetConnectionInfo(rptDoc.Database.Tables);

            rptDoc.SetParameterValue("@profile_id", Request.QueryString["id"].ToString());

            //rptDoc.PrintToPrinter(1, false, 1, 1);

            rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("~/export/profile.pdf"));
            ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/profile.pdf');popup.focus();", true);
        }
    
    }
    protected void LoadImage(DataRow r, string strImageField, string strPath)
    {
        try
        {
            FileStream f = new FileStream(strPath, FileMode.Open, FileAccess.Read);
            byte[] img = new byte[f.Length];
            f.Read(img, 0, Convert.ToInt32(f.Length));
            f.Close();
            r[strImageField] = img;
        }

        catch (Exception ex)
        {
            Response.Write("<font color=red>" + ex.Message + "</font>");
        }

    }
    protected void Load_DataSet2Report()
    {
        //DataSet ds;
        //TypedDataSet ds = new TypedDataSet();

        profile ds = new profile();
        string strID = Request.QueryString["id"].ToString();
        Guid id = new Guid(strID);
        //ds = SqlHelper.ExecuteDataset(Common.strConnect, "Get_Profile", new object[] {id});
        SqlConnection cn = new SqlConnection(Common.strConnect);
        SqlCommand sqlCommand = new SqlCommand("Get_Profile",cn);
        sqlCommand.CommandType = CommandType.StoredProcedure;
        SqlParameter param=  sqlCommand.Parameters.Add("profile_id", SqlDbType.UniqueIdentifier);
        param.Value=id;
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
        sqlDataAdapter.Fill(ds.Tables[0]);


        if (ds.Tables[0].Rows[0]["image_URL"].ToString() != "")
        {
            string s = Server.MapPath(ds.Tables[0].Rows[0]["image_URL"].ToString());

            if (File.Exists(s))
                LoadImage(ds.Tables[0].Rows[0], "image_profile", s);
        }
        rptDoc = new ReportDocument();
        rptDoc.Load(Server.MapPath("~/reports/rpt_profile8.rpt"));
        rptDoc.SetDataSource(ds.Tables[0]);
        rptViewProfile.ReportSource = rptDoc;



    }

    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();

        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
}
